<template>
  <basic-container>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><i class="el-icon-setting"></i> 商品管理</el-breadcrumb-item>
        <el-breadcrumb-item v-if="pageType == 'add'"><i class="el-icon-date"></i> 新增分类</el-breadcrumb-item>
        <el-breadcrumb-item v-if="pageType == 'edit'"><i class="el-icon-date"></i> 编辑分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container category-wrap">
      <el-form :model="categoryForm" label-width="90px" label-position="right" :rules="rules">

        <el-form-item label="分类权重">
          <el-input-number :max="99999" :min="0" v-model="categoryForm.sort" class="form-input"></el-input-number>
        </el-form-item>

        <el-form-item label="分类名称" required prop="name">
          <el-input type="text" style="width: 200px;" v-model="categoryForm.name" class="form-input" clearable></el-input>
        </el-form-item>

        <el-form-item v-if="showList" label="所属分类" required>
          <el-select v-model="categoryForm.parentId" @change="selectVal" clearable style="float: left" placeholder="请选择">
            <el-option
              v-for="item in parents"
              :key="item.name"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否显示" required style="text-align: left">
          <el-switch v-model="categoryForm.categoryType" :active-value="'1'" :inactive-value="'0'">
          </el-switch>
        </el-form-item>

        <el-form-item label="分类图标">
          <div>
            <div v-if="categoryForm.logo" class="thumb">
              <img :src="categoryForm.logo"/>
              <div class="hover_show">
                <i class="el-icon-delete" @click="handlePictureRemove(categoryForm.logo)"></i>
              </div>
            </div>
            <div class="select_img" @click="sel_img(categoryForm.logo)">
              <i class="el-icon-plus"></i>
            </div>
          </div>
        </el-form-item>

        <el-form-item>
          <div>
            <el-button type="primary" size="medium" @click="modifyGoodsCategory">保存</el-button>
            <el-button type="default" size="medium" @click="close">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog title="选择图片" :visible.sync="selectImgFlag" style="text-align: left;">
      <sel-img-photogallery ref="selImg" :type="imgType" @select="onSelectImgs"></sel-img-photogallery>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getCategoryTree, addCategory, editCategory, getCategoryDetail} from "../../api/goods/goodscategory";
  import selImgPhotogallery from '../../components/pds/common/sel_img_photogallery';

  export default {
    name: "add_category",
    data() {
      return {
        rules: {
          name:[
            {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
            }
          ]
        },
        pageType: 'add',
        categoryForm: {
          shopId: '',
          name: '',
          parentId: 0,
          sort: 100,
          logo: '',
          categoryType: '1'
        },
        parents: [],//分类列表
        //图片上传
        selectImgFlag: false,
        imgType: '',
        showList: false,
        shopId: '',
        parentId: '',
        categoryId: '',
        query: '',
        path: ''
      }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        vm.shopId = vm.$route.query.res.shopId;
        vm.parentId = vm.$route.query.res.parentId;
        vm.categoryId = vm.$route.query.res.categoryId;
        vm.query = vm.$route.query.res
        vm.path = vm.$route.path
        vm.changeType()
        vm.getCategoryList()
        vm.showCategory()
      })
    },
    components: {
      selImgPhotogallery
    },
    // created() {
    //   this.changeType()
    //   this.getCategoryList()
    //   this.showCategory()
    // },
    methods: {
      //  获取分类列表
      getCategoryList() {
        const shopId = this.shopId
        const pageType = this.pageType
        if(pageType == 'edit') {
          getCategoryTree({shopId: shopId,allShow: true}).then(res => {

            this.parents = res.data.data
          })
        }else {
          getCategoryTree({shopId: shopId}).then(res => {
            this.parents = res.data.data
          })
        }
      },
      // 获取分类详情
      getCategoryDetail() {
        const categoryId = this.categoryId
        getCategoryDetail(categoryId).then(res => {
          const data = res.data.data
          console.log(data);
          if(data.parentId == '0') {
            data.parentId = ''
          }
          this.initData(data)
          this.categoryForm.categoryId = res.data.data.categoryId
        })
      },
      //是否显示分类类表
      showCategory() {
        let parentId = this.parentId
        let categoryId = this.categoryId
        if(parentId || categoryId) {
          this.showList = true
        }
      },
      //  选择父分类
      selectVal: function (id) {
        // console.log(id);
        this.parents.forEach(item => {
          if (item.id == id) {
            this.categoryForm.parentId = item.id;
          }
        })
      },
      //选择类型
      changeType() {
        let query = this.query
        if (query != null) {
          this.initData(query)
        }
        // this.path = this.$route.path
        if (this.path == '/goods/addCategory') {
          this.pageType = 'add'
        } else {
          this.pageType = 'edit'
          this.getCategoryDetail()
        }
      },
      initData(data) {
        this.categoryForm.shopId = data.shopId
        this.categoryForm.parentId = data.parentId || ''
        this.categoryForm.name = data.categoryName || data.name || ''
        this.categoryForm.sort = data.sort || 100
        this.categoryForm.logo = data.logo || ''
        this.categoryForm.categoryType = data.categoryType || '1'
      },
      //删除图片
      handlePictureRemove(img) {
        this.categoryForm.logo = ''
      },
      //打开组件
      sel_img(img) {
        this.imgType = "03";
        this.selectImgFlag = true;
      },
      //选择的图片
      onSelectImgs(imgArry) {
        // console.log(imgArry[0].picUrl);
        this.categoryForm.logo = imgArry[0].picUrl
        this.selectImgFlag = false;
      },
      //  保存
      modifyGoodsCategory() {
        console.log(this.pageType);
        if(this.pageType == 'add') {
          console.log(this.categoryForm);
          addCategory(this.categoryForm).then(res => {
            this.$message.success("添加成功")
            this.close()
          })
        }else {
          editCategory(this.categoryForm).then(res => {
            this.$message.success("编辑成功")
            this.close()
          })
        }

      },
      close(time = 1000) {
        let that = this;
        setTimeout(function () {
          that.$store.commit('DEL_ONE_TAG', that.$route);
          that.$router.go(-1);
        }, time);
      }

    }
  }
</script>

<style scoped>
  .crumbs {
    margin-top: 20px;
    margin-left: 10px;
  }

  .category-wrap {
    padding: 20px;
  }

  .category-uploader-icon {
    line-height: 150px;
  }

  .parent-category-name {
    color: #409EFF;
    background: #ecf5ff;
    border-color: #b3d8ff;
    text-align: left;
    padding-left: 15px;

  }

  .category-logo {
    width: 150px;
    height: 150px;
  }

  .category-logo-uploader .el-upload--text {
    width: 150px;
    height: 150px;
  }

  .thumb {
    width: 150px;
    height: 150px;
    position: relative;
    line-height: 146px;
    float: left;
    margin-right: 15px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #c0ccda;
  }

  .thumb img {
    width: 100%;
    height: 100%;
  }

  .thumb .hover_show {
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    display: none;
    text-align: center;
  }

  .thumb:hover .hover_show {
    display: block;
  }

  .thumb .hover_show i {
    color: #fff;
    margin: 0 10px;
    cursor: pointer;
  }

  .select_img {
    width: 148px;
    height: 148px;
    line-height: 146px;
    border: 1px dashed #c0ccda;
    background-color: #fbfdff;
    font-size: 28px;
    color: #8c939d;
    cursor: pointer;
    border-radius: 6px;
    text-align: center;
    float: left;
  }
</style>
